home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9101 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Very Newbie question on pointers
  5. Date: 6 Mar 1996 21:34:14 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hlsgmINNcgt@keats.ugrad.cs.ubc.ca>
  8. References: <bantolov-0603960018080001@bantolov.seanet.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <bantolov-0603960018080001@bantolov.seanet.com>,
  12. Bruce Antolovich <bantolov@bantolov.seanet.com> wrote:
  13. >I hope that this is the right place to post such a question; if not, sorry
  14. >for the wasted bandspace.
  15. >
  16. >I am very new to programming in C but have a large background in FORTRAN.
  17. >(please no snickers) I am having a problem with the memory location of
  18. >variables as described by pointers. I define several variables and try to
  19. >get their address in memory. Unfortunately, my code gives me the same
  20. >address for all variables! Any clues as to where I'm going wrong would be
  21. >very appreciated.
  22. >
  23. >I don't think that it matters but I'm using Metrowerks CW on a PowerMac 6100/60.
  24.  
  25. It does. You are using %d in printf() to print pointer values. This is not
  26. right because pointers are not integers, and certainly don't even have to be
  27. the same bit size.
  28.  
  29. It's possible that the ``int'' type on your (POWER PC) compiler is 32 bits, and
  30. that pointers are 64 bit. If that is the case, printf("%d",pointer_var) will
  31. just look at the top 32 bits of the 64-bit pointer. Of course these will likely
  32. be the same for two variables that are declared side by side in the same stack
  33. frame!
  34.  
  35. Of course, this is just an explanation of what might be happening in your
  36. program---I certainly don't mean to imply that if you are on a system where
  37. both ints and all pointers are the same size, you are free to treat them as
  38. interchangeable.  The conversion of integral types to pointers and back is
  39. governed by an implementation-defined mapping. The integral type has to have a
  40. sufficient size to hold the pointer (this size is implementation defined).  If
  41. you convert a pointer to a sufficiently wide integer, and then back to a
  42. pointer of the same type, you recover the pointer. (K&R2 A6.6)
  43.  
  44. To print pointer values, the proper conversion argument for printf() is
  45. "%p", not "%d".
  46.  
  47. Hope this helps!
  48.  
  49. BTW, Fortran is cool! :)
  50. -- 
  51.  
  52.